/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.ws;

import java.util.UUID;
import javax.inject.Inject;
import javax.jws.WebService;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.ExchangePattern;
import org.apache.camel.cdi.CdiCamelContext;
import org.apache.cxf.annotations.SchemaValidation;
import riv.service.catalogue._1.ResultCodeEnum;
import riv.service.catalogue._1.ResultLevelEnum;
import riv.service.catalogue._1.ResultType;
import se.riv.service.catalogue.getproductcatalogue.v1.rivtabp21.GetProductCatalogueResponderInterface;
import se.riv.service.catalogue.getproductcatalogueresponder.v1.GetProductCatalogueResponseType;
import se.riv.service.catalogue.getproductcatalogueresponder.v1.GetProductCatalogueType;
import se.sll.service.catalogue.common.property.CatalogProperties;

/**
 * RIVTA implementation for service.catalogue.getproductcatalogue
 *
 * @author khaleddaham
 */
@Slf4j
@WebService(
        endpointInterface = "se.riv.service.catalogue.getproductcatalogue.v1.rivtabp21.GetProductCatalogueResponderInterface",
        serviceName = "GetProductCatalogue")
@SchemaValidation
public class GetProductCatalogue implements GetProductCatalogueResponderInterface {

    @Inject
    private CdiCamelContext camelCtx;

    @Override
    public GetProductCatalogueResponseType getProductCatalogue(String string, GetProductCatalogueType request) {
        UUID logId = UUID.randomUUID();
        log.info("logId: {}, request: {}", logId.toString(), request.toString());
        GetProductCatalogueResponseType response = new GetProductCatalogueResponseType();
        if (!request.getCodeSystem().contains(CatalogProperties.getProperty(CatalogProperties.NAME.PRODUCT_OID)
        ) && request.getCodeSystem().size() > 0) {
            ResultType resultType = new ResultType();
            resultType.setLevel(ResultLevelEnum.OK);
            resultType.setCode(ResultCodeEnum.INVALID_REQUEST);
            resultType.setMessage("Request contains no valid product catalogue codeSystem(s).");
            response.setResult(resultType);
            return response;
        }
        try {
            response = (GetProductCatalogueResponseType) camelCtx.createProducerTemplate().sendBodyAndHeader("direct:start:listproducts",
                    ExchangePattern.InOut, "", "", "");
            log.debug("Found {} products catalogues.", response.getProductCatalogue().size());
        } catch (CamelExecutionException ex) {
            log.error(ex.getMessage(), ex);
            return response;
        }
        if (response.getResult() == null) {
            ResultType resultType = new ResultType();
            resultType.setLevel(ResultLevelEnum.OK);
            response.setResult(resultType);
        }
//        response.getResult().setCode(ResultCodeEnum.OK_REQUEST);
        response.getResult().setLogId(logId.toString());
        return response;
    }
}
